//
//  AppUser.swift
//  手账
//
//  Created by zhuxiaoxiang on 2018/12/15.
//  Copyright © 2018年 zhuxiaoxiang. All rights reserved.
//

import Foundation

struct AppUser {
    
    enum LoginState {
        case login
        case logout
        
        var loginStateDescription: String {
            get {
                switch self {
                case .login:
                    return "登录状态"
                case .logout:
                    return "登出状态"
                }
            }
        }
        
        var isLogin: Bool {
            get {
                switch self {
                case .login:
                    return true
                case .logout:
                    return false
                }
            }
        }
    }
    
    static var sharedInstance = AppUser()
    var loginState: LoginState {
        get {
            return self.phone == nil ? .logout : .login
        }
        set {
            
        }
    }
    
    var phone: String? {
        get {
            return UserDefaults.standard.value(forKey: "phone") as? String
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "phone")
        }
    }
    
    mutating func login(_ phone: String?) {
        self.phone = phone
        self.loginState = .login
    }
    
     mutating func logout() {
        self.phone = nil
        self.loginState = .logout
    }
    
    static func presentLoginVC() {
        let loginVC = XXNavigationController(rootViewController: LoginViewController())
        UIApplication.shared.keyWindow?.rootViewController?.present(loginVC, animated: true, completion: nil)
    }
}
